pipeline {
    //选择节点
    agent { label '4.200' } 
    stages {
        // 获取对应git参数
        stage('Select Server'){
            steps {
                script{
                    //修改变量
                    server_name = "core"
                    zh_name = project_name.split(':')[0]
                    project_name = project_name.split(':')[1]
                    env_name = "prod"
                    code_version = "NO"
                    //读取变量
                    devops_dir="/data/jenkins/workspace/devops"
                    jenkins_shell="${devops_dir}/jenkins/shell"
                    wethink_renew="${jenkins_shell}/wethink_prod.sh"
                    notice_sh="${jenkins_shell}/notice_jenkins.py"
                    env_dir="${devops_dir}/conf/env"
                    env_file="${env_dir}/${env_name}_wethink_env.csv"
                    notice_file="${env_dir}/dingding.csv"
                    project_pwd=sh(script: "cat ${env_file}  | awk -F ',' -v project_name=${project_name} -v server_name=${server_name} '{ if (\$2 == project_name && \$3 == server_name) print \$4}'", returnStdout: true)
                    project_noticeToken=sh(script: "grep ${project_name} ${notice_file} |  awk -F ',' '{print \$2}'", returnStdout: true)
                    gitlab_url="http://xxx" 
                    project_url="${gitlab_url}/${project_pwd}"
                    project_dir="/data/jenkins/workspace/${env_name}/${project_name}_${server_name}"
                    sh "mkdir -p ${project_dir}"
                }
            }
        }
        stage('拉代码') {
            steps {
                checkout([$class: 'GitSCM', branches: [[name: project_branch]], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: project_dir]], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'gitlab', url: project_url]]])
            }
        }
        stage('编译') {
            steps {
                sh encoding: 'UTF-8', script:"/bin/bash -xe ${wethink_renew} complish ${project_name}  ${server_name} ${env_name} ${code_version}"
            }
        }
        stage('灰度发布') {
            steps {
                sh encoding: 'UTF-8', script:"/bin/bash ${update_shell} update ${server} ${PROJECT_DIR}  ${env_name}  huidu"
            }
        }
        stage('是否发布剩余服务器') {
            //当服务为core时,发布剩余服务器
            when{
                environment name:"server_name", value:"core"
            }
            steps {
                input message:"是否发布${zh_name}的剩余服务器", ok: "通过", submitter: "xx"
                sh encoding: 'UTF-8', script:"/bin/bash ${update_shell} update ${server} ${PROJECT_DIR}  ${env_name}  all"
            }
        }  
    }
    //通知
    post {
        success { 
            sh encoding: 'UTF-8', script:"python3 ${notice_sh} 成功 ${zh_name}  ${server_name}  ${env_name}   ${env.BUILD_NUMBER}  ${project_noticeToken}"
        }
        failure {
            sh encoding: 'UTF-8', script:"python3 ${notice_sh} 失败 ${zh_name}  ${server_name}  ${env_name}   ${env.BUILD_NUMBER}  ${project_noticeToken}" 
        }
    }
}